package com.chuangke.common.poitl;

import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author admin
 */
public class CkDynamicTableRenderPolicy extends DynamicTableRenderPolicy {

	@Override
	public void render(XWPFTable table, Object data) {
		if (null == data) {
			return;
		}

		DynamicTableData detailData = (DynamicTableData) data;

		// 商品订单详情列表数据 循环渲染
		List<RowRenderData> dataList = detailData.getRowDatas();

		if (CollectionUtils.isEmpty(dataList)) {
			return;
		}

		/** 渲染表格 */
		int colSize = dataList.get(0).getCells().size();
		for (int i = dataList.size() - 1; i >= 0; i--) {
			XWPFTableRow insertNewTableRow = table.insertNewTableRow(detailData.getFirstDataRowIndex());
			// 循环插入列
			for (int j = 0; j < colSize; j++) {
				XWPFTableCell cell = insertNewTableRow.createCell();
				cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); // 垂直居中
			}
			// 渲染单行数据
			try {
				TableRenderPolicy.Helper.renderRow(insertNewTableRow, dataList.get(i));
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

        /* 处理合并行 */
		renderMergeRow(table, detailData);
	}

	private void renderMergeRow(XWPFTable table, DynamicTableData detailData) {
		List<Integer> mergeColIndexs = detailData.getMergeColIndexs();
		if (CollectionUtils.isEmpty(mergeColIndexs)) {
			return;
		}

		Map<Integer, MergeCellInfo> mergeMap = new HashMap<>();
		for (int i = 0; i < detailData.getRowDatas().size(); i++) {
			for (Integer colIndex : mergeColIndexs) {
				TextRenderData trd = ((TextRenderData) detailData.getRowDatas().get(i).getCells().get(colIndex)
						.getParagraphs().get(0).getContents().get(0));
				String currentColValue = trd.getText();

				MergeCellInfo mcInfo = mergeMap.get(colIndex);
				if (mcInfo == null) {
					mcInfo = new MergeCellInfo();
					mcInfo.setColValue(currentColValue);
					mcInfo.setStartRow(i + detailData.getFirstDataRowIndex());
					mcInfo.setEndRow(i + detailData.getFirstDataRowIndex());

					mergeMap.put(colIndex, mcInfo);
					continue;
				}

				if (Objects.equals(currentColValue, mcInfo.getColValue())) {
					mcInfo.setEndRow(i + detailData.getFirstDataRowIndex());
				}

				// 1、当前单元格的值和上一个单元格的值不一致的时候，需要将之前值一致的单元格进行合并
				// 2.如果当前为表格的最后一行，需要将之前值一致的单元格进行合并
				if ((!Objects.equals(currentColValue, mcInfo.getColValue())
						|| i == (detailData.getRowDatas().size() - 1)) && mcInfo.getStartRow() != mcInfo.getEndRow()) {
					TableTools.mergeCellsVertically(table, colIndex, mcInfo.getStartRow(), mcInfo.getEndRow());

					// 之前的单元格合并之后，重新初始化
					mcInfo.setColValue(currentColValue);
					mcInfo.setStartRow(i + detailData.getFirstDataRowIndex());
					mcInfo.setEndRow(i + detailData.getFirstDataRowIndex());
				}
			}
		}
	}

	public static class MergeCellInfo {
		private String colValue;// 单元格值
		private int startRow;// 开始合并行索引
		private int endRow;// 结束合并行索引

		public String getColValue() {
			return colValue;
		}

		public void setColValue(String colValue) {
			this.colValue = colValue;
		}

		public int getStartRow() {
			return startRow;
		}

		public void setStartRow(int startRow) {
			this.startRow = startRow;
		}

		public int getEndRow() {
			return endRow;
		}

		public void setEndRow(int endRow) {
			this.endRow = endRow;
		}

	}
}